package com.cn.play.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

/**
 * Created by chenyuncong on 2018/10/15.
 */
@Controller
@RequestMapping("play")
public class PlayWeb {

    /**
     * 视频流读取
     * @param id
     * @param response
     * @throws Exception
     */
    @RequestMapping("/video.do")
    public @ResponseBody void video(String id, HttpServletResponse response)throws Exception{
        File file = new File("D:/0-1-20181011000038_A.mp4");
        FileInputStream in = new FileInputStream(file);
        ServletOutputStream out = response.getOutputStream();
        byte[] b = null;
        while(in.available() >0) {
            if(in.available()>10240) {
                b = new byte[10240];
            }else {
                b = new byte[in.available()];
            }
            in.read(b, 0, b.length);
            out.write(b, 0, b.length);
        }
        in.close();
        out.flush();
        out.close();
    }

}
